//
//  ListaObjetoViagemViewController.m
//  ViagemBem
//
//  Created by Michel de Sousa Carvalho on 11/11/13.
//  Copyright (c) 2013 Michel de Sousa Carvalho. All rights reserved.
//

#import "ListaObjetoViagemViewController.h"

@interface ListaObjetoViagemViewController ()

@property (weak, nonatomic) IBOutlet UITableView *tableListarObjetos;
@property (nonatomic, strong) NSArray *listaObjetosViagem;
@property (weak, nonatomic) IBOutlet UITextField *txtTipoObjeto;
@end

@implementation ListaObjetoViagemViewController

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return NO;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tableListarObjetos.delegate = self;
    self.tableListarObjetos.dataSource = self;
    self.txtTipoObjeto.delegate = self;

    // Do any additional setup after loading the view.
}

-(void) viewWillAppear:(BOOL)animated{
    self.listaObjetosViagem = [TipoObjetosViagemDAO listarTipoObjetos];
    [self.tableListarObjetos reloadData];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //    return [self.produto.nomesDosProdutosNaLista count];
    return [self.listaObjetosViagem count];
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
        cell.backgroundColor= [UIColor clearColor];
    }
    
    NSString *tipo = ((TipoObjeto *)self.listaObjetosViagem[[indexPath row]]).tipo;
    
//    NSString *valor = [NSString stringWithFormat:@"R$%@",((TipoO *)self.listaGastos[[indexPath row]]).valor];
    
    cell.textLabel.text = tipo;
    
    NSArray *selectedRowsIndexPath = [tableView indexPathsForSelectedRows];
    cell.accessoryType = UITableViewCellAccessoryNone;
    for (NSIndexPath *path in selectedRowsIndexPath){
        if (path.row == indexPath.row) {
            cell.accessoryType=UITableViewCellAccessoryCheckmark;
        }
    }
    
//    tableView.allowsMultipleAnswersAccepted;
//    cell.detailTextLabel.text = valor;
    
    return cell;
}
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
    
//    NSInteger linhaSelecionada = indexPath.row;
//    Gasto *gasto = [self.listaGastos objectAtIndex:linhaSelecionada];
//    [self performSegueWithIdentifier:@"segueDetalhesGasto" sender:gasto];
    if (tableViewCell.accessoryType == UITableViewCellAccessoryCheckmark) {
        //tableViewCell.accessoryType = UITableViewCellAccessoryCheckmark;
        tableViewCell.accessoryType = UITableViewCellAccessoryNone;
    }
    else{
        tableViewCell.accessoryType=UITableViewCellAccessoryCheckmark;
    }

    
}
- (IBAction)btnAdicionarTipoObjeto:(id)sender {
    if(![self.txtTipoObjeto.text  isEqual: @" "] && ![self.txtTipoObjeto.text isEqual:nil]){
        [TipoObjetosViagemDAO inserirTipoObjeto: self.txtTipoObjeto.text];
    }

}

@end
